import {createSlice, PayloadAction} from "@reduxjs/toolkit";

import {StateBaseType} from "redux/StateBaseType";
import {ReduxStatus} from "enum/ReduxStatus";

import {EngineDataContrast} from "type/EngineDataContrast";

import LogFactory from "../../log/LogFactory";

const log = LogFactory.getLog("EngineDataContrastReducer") ;

/**
 * 零件信息Redux状态类型声明
 */
export type EngineDataState = {
    data: EngineDataContrast[] ;
    loading: boolean ;
    total: number ;
    // current: number ;
    // pageSize: number ;
    // search: Partial<EngineDataContrast> ;
} ;

// redux 状态初始化
const initialState: EngineDataState = {
    data: [],
    loading: false,
    total: 0,
};

// 创建reducer数据片
export const engineDataReducer = createSlice({
    name: "engineDataContrast",
    initialState,
    reducers: {
        listData: (state, action: PayloadAction<EngineDataContrast[]>) => {
            state.data = action.payload ;
            state.total = action.payload.length ;
        },
        addData: (state, action: PayloadAction<EngineDataContrast>) => {
            state.data.unshift(action.payload) ;
            state.total = state.total + 1 ;
        }
    } ,
});

export const {listData,addData} = engineDataReducer.actions ;
export default engineDataReducer.reducer ;
